package com.lesserpand.rabbitmq.threadpool;

import com.alibaba.fastjson.JSONObject;
import com.lesserpand.rabbitmq.threadpool.thread.SyncExecutor;
import org.redisson.api.RDeque;
import org.redisson.api.RQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Description TODO
 * @ClassName StartThreadPool
 * @Author pand
 * @Date 2019/4/8 22:31
 * @Version 1.0
 */
public class StartThreadPool {

    private static final Logger LOGGER = LoggerFactory.getLogger(StartThreadPool.class);


    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private SyncExecutor syncExecutor;

    /**
     * 线程池插任务
     */
    // @Scheduled(cron = "*/5 * * * * *")
    public void dealCmccDataTask() {
        SyncRunnable syncRunnable = new SyncRunnable(stringRedisTemplate);
        syncExecutor.startUpThread(syncRunnable);

        this.isFinish();
    }

    /**
     * 监听线程池任务是否执行完
     */
    public void isFinish() {
        ThreadPoolExecutor executorService = syncExecutor.getExecutorService();
        executorService.shutdown();
        while (true) {
            if (executorService.isTerminated()) {
                LOGGER.info("线程池任务结束了！");
            }
            try {
                // 停1分钟
                Thread.sleep(60000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
